﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3.SeveralStringSequence
{
    class Program
    {
        static void Main(string[] args)
        {
            //TODO: Malko ostana

            /*as sets of several neighbor elements located
             * on the same line, column or diagonal.
             * Write a program that finds the longest sequence of equal strings in the matrix. 
             * /*/

            string[,] words = {
                            {"ha", "ha", "si", "hi", "ha", "e"},
                            {"ha", "he", "ai", "hi", "ho", "b"},
                            {"ha", "hi", "si", "ha", "ha", "d"},
                            {"ha", "he", "hi", "ho", "ho", "a"},
            };
            int counteronline = 0;
            int bestseqonline = 0;
            string bestWordonline = words[0, 0];

            
            // on same line
            
            
            for (int row = 0; row < words.GetLength(0); row++)
            {
                for (int col = 0; col <words.GetLength(1); col++)
                {
                    if (words[row, row] == words[row, col])
                    {
                        counteronline++;
                        if (counteronline >= bestseqonline)
                        {
                            bestseqonline = counteronline;
                            bestWordonline = words[row, col];
                        }
                    }
                    else
                    {
                        counteronline = 0;
                    }
                }
            }
            Console.WriteLine("Best sequence of: {0} times - string:{1} on the same line", bestseqonline, bestWordonline);
             int counterdiag = 0;
            int bestseqdiag = 0;
            string bestWorddiag = words[0, 0];
            // diagonal 
            if (words.GetLength(0) >= words.GetLength(1))
            {
                for (int row = 0; row < words.GetLength(0); row++)
                {
                    //word = words[row, row];
                    for (int col = row; col < words.GetLength(0); col++)
                    {
                        if (words[col, col] == words[col, col + 1])
                        {
                            counterdiag++;
                            if (counterdiag >= bestseqdiag)
                            {
                                bestseqdiag = counterdiag;
                                bestWorddiag = words[col, col];
                            }
                        }
                        else
                        {
                            counterdiag = 0;
                        }
                    }

                }
            }
            else
            {
                string lastwordchecked = words[0, 0];
                for (int row = 0; row < words.GetLength(0); row++)
                {
                    //word = words[row, row];
                    for (int col = row; col < words.GetLength(1); col++)
                    {
                        
                        if (lastwordchecked == words[col, col + 1])
                        {
                            counterdiag++;
                            if (counterdiag >= bestseqdiag)
                            {
                                bestseqdiag = counterdiag;
                                bestWorddiag = words[col, col];
                            }
                            lastwordchecked = words[col, col + 1];
                        }
                        else
                        {
                            counterdiag = 0;
                            lastwordchecked = words[col, col + 1];
                        }
                    }

                }
            }
            Console.WriteLine("Best sequence of: {0} times - string:{1} - diagonal", bestseqdiag, bestWorddiag);
            
            int counteroncolumn = 0;
            int bestseqoncolumn = 0;
            string bestWordoncolumn = words[0, 0];


            // on same column
            //words.GetLength(1) - shte dade kolko koloni
            //words.GetLength(0) - redove
            // [0->6,0->4]
            for (int col = 0; col < words.GetLength(1); col++)
            {
                for(int row=0; row<words.GetLength(0)-1; row++)
                {
                    if (words.GetLength(0) - 1 == words.GetLength(0))
                    {
                        counteroncolumn += 1;
                        if (counteroncolumn >= bestseqoncolumn)
                        {
                            bestseqoncolumn = counteroncolumn - 1;
                            bestWordoncolumn = words[row, col];
                        }
                    }
                    else if (words[row, col] == words[row+1, col])
                    {
                        counteroncolumn+=2;
                        if (counteroncolumn >= bestseqoncolumn)
                        {
                            bestseqoncolumn = counteroncolumn - 1;
                            bestWordoncolumn = words[row, col];
                        }
                    }
                    else
                    {
                        counteroncolumn = 0;
                    }
                }
            }
            Console.WriteLine("Best sequence of: {0} times - string:{1} on the same column", bestseqoncolumn, bestWordoncolumn);
        }
    }
}

